package com.justgame.project.cloud.common.util;

import com.justgame.project.cloud.common.constant.Const;
import org.apache.shiro.codec.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 加密解密
 */
@Component
public class Encryption {

    //参数分别代表 算法名称/加密模式/数据填充方式
    private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";
    private static Logger logger = LoggerFactory.getLogger(Encryption.class);

    public static StringBuffer PasswordMD5(String str) {
        return MD5(str + Const.DEFAULT_SALT_PASSWORD);
    }

    public static StringBuffer MD5(String str) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] b = digest.digest(str.getBytes());
            return new StringBuffer().append(Hex.encode(b));
        } catch (NoSuchAlgorithmException e1) {
            logger.error("MD5加密失败" + e1);
        }
        return null;
    }

    public static StringBuffer PasswordMD5(String str, String salt) {
        try {
            String Salted = str + salt;
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] b = digest.digest(Salted.getBytes());
            return new StringBuffer().append(Hex.encode(b));
        } catch (NoSuchAlgorithmException e1) {
            logger.error("MD5加密失败" + e1);
        }
        return null;
    }

    /**
     * Base64 解码
     *
     * @param str
     * @return
     */
    public static String Base64Decode(String str) {
        try {
            return new String(Base64Utils.decode(str.getBytes()));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        }
        return "";
    }

    /**
     * Base64 加密
     *
     * @param str
     * @return
     */
    public static String Base64Encode(String str) {
        try {
            return Base64Utils.encodeToString(str.getBytes());
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        }
        return "";
    }

    /**
     * 加密
     *
     * @param content    加密的字符串
     * @param encryptKey key值
     * @return
     * @throws Exception
     */
    public static String encrypt(String content, String encryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));
        byte[] b = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
        // 采用base64算法进行转码,避免出现中文乱码
        return Base64Utils.encodeToString(b);

    }

    /**
     * 解密
     *
     * @param encryptStr 解密的字符串
     * @param decryptKey 解密的key值
     * @return
     * @throws Exception
     */
    public static String decrypt(String encryptStr, String decryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
        // 采用base64算法进行转码,避免出现中文乱码
        byte[] encryptBytes = Base64Utils.decodeFromString(encryptStr);
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes);
    }

}
